package com.qqtech.qquan.mall.service;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.cache.MallSysParamCache;
import com.qqtech.qquan.mall.dao.MallShopDao;
import com.qqtech.qquan.mall.enums.MallShopAuditStatusDictEnum;
import com.qqtech.qquan.mall.enums.MallShopTypeDictEnum;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.model.MallShopCommunity;
import com.qqtech.qquan.mall.model.MallShopQuery;
import com.qqtech.qquan.mall.model.MallShopVo;

@Service
public class MallShopServiceImpl extends BaseServiceImpl<MallShop>
		implements MallShopService {
	public static final Logger log = LoggerFactory.getLogger(MallShopServiceImpl.class);
	
	@Resource
	private MallShopDao mallShopDao;
	
	@Resource
	private MallShopCommunityService mallShopCommunityService;
	@Resource
	private MallSysParamCache mallSysParamCache;

	@Override
	protected BaseDao<MallShop> getBaseDao() {
		return mallShopDao;
	}

	@Override
	public Page<MallShopVo> queryPageListAdmin(MallShop query, Pageable pageable) {
		return mallShopDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public MallShopVo getByCreatorId(Integer creatorId) {
		if (creatorId == null) {
			return null;
		}
		MallShopQuery q = new MallShopQuery();
		q.setCreatorId(creatorId);
		List<MallShopVo> list = mallShopDao.queryList(q);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean isShopOwner(Integer userId, Integer shopId) {
		if(userId==null || userId ==null){
			return false;
		}
		MallShopQuery shop = new MallShopQuery();
		shop.setId(shopId);
		shop.setCreatorId(userId);
		return mallShopDao.queryCount(shop) > 0;
	}

	@Override
	public int getCountShopByCommunityId(Integer communityId, Integer style) {
		if (communityId == null) {
			return 0;
		}
		MallShopQuery q = new MallShopQuery();
		q.setCommunityId(communityId);
		q.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		q.setStyle(style);
		q.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		return mallShopDao.queryCount(q);
	}

	@Override
	public List<MallShopVo> listByHasRecommend(Integer communityId, Integer style, Integer count) {
		MallShopQuery q = new MallShopQuery();
		q.setCommunityId(communityId);
		q.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		q.setStyle(style);
		q.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		q.setHasRecommend(YesNoEnum.YES.getCode());
		if (count == null || count.intValue() <= 0) {
			return mallShopDao.queryList(q);
		} else {
			Pageable pageKit = new PageKit(1, count, new PageOrder(
					Direction.DESC, "id"));
			return mallShopDao.queryList(q,pageKit);
		}
	}

	@Override
	public int addSaleOrderCount(Integer shopId) {
		return mallShopDao.addSaleOrderCount(shopId);
	}

	@Override
	public int subSaleOrderCount(Integer shopId) {
		return mallShopDao.subSaleOrderCount(shopId);
	}

	@Override
	public List<MallShopVo> queryListByCollect(Integer userId, Integer communityId, Integer type, Integer style, Integer auditStatus) {
		if (userId == null) {
			return null;
		}
		return mallShopDao.queryListByCollect(userId, communityId, type, style, auditStatus);
	}

	@Override
	public List<MallShopVo> queryListByCollectV1(Integer userId, Integer communityId, Integer type, Integer style, Integer auditStatus,BigDecimal x,BigDecimal y) {
		if (userId == null) {
			return null;
		}
		return mallShopDao.queryListByCollectV1(userId, communityId, type, style, auditStatus, x, y);
	}
	
	@Override
	public void handleSave(MallShop shop) {
		int id = mallShopDao.insert(shop);
		if (id > 0) {
			MallShopCommunity mallShopCommunity = new MallShopCommunity();
			mallShopCommunity.setCommunityId(shop.getCommunityId());
			mallShopCommunity.setShopId(id);
			mallShopCommunity.setNewAt(shop.getNewAt());
			mallShopCommunityService.insert(mallShopCommunity);
		}
	}


	@Override
	public List<MallShopVo> queryALl() {
		return mallShopDao.queryAllShop();
	}
	
	@Override
	public Page<MallShopVo> queryDistancePage(MallShopQuery query, Pageable pageable) {
		return mallShopDao.queryDistancePage(query, pageable);
	}
	
	@Override
	public Integer getServiceCommunityDistance(String key) {
		return mallSysParamCache.getInt(key);
	}
}